package com.campus.counseling.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.counseling.model.common.Result;
import com.campus.counseling.model.dto.TestQuestionDTO;
import com.campus.counseling.model.query.TestQuestionQuery;
import com.campus.counseling.model.vo.TestQuestionVO;
import com.campus.counseling.service.TestQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "测试题目管理")
@RestController
@RequestMapping("/api/test-questions")
@RequiredArgsConstructor
public class TestQuestionController {

    private final TestQuestionService testQuestionService;

    @Operation(summary = "获取题目列表")
    @GetMapping
    public Result<Page<TestQuestionVO>> list(TestQuestionQuery query) {
        return Result.success(testQuestionService.list(query));
    }

    @Operation(summary = "获取题目详情")
    @GetMapping("/{id}")
    public Result<TestQuestionVO> getQuestion(@PathVariable Long id) {
        return Result.success(testQuestionService.getQuestionById(id));
    }

    @Operation(summary = "创建题目")
    @PostMapping
    public ResponseEntity<Void> addQuestion(@RequestBody TestQuestionDTO dto) {
        testQuestionService.add(dto);
        return ResponseEntity.ok().build();
    }

    @Operation(summary = "更新题目")
    @PutMapping("/{id}")
    public Result<Boolean> updateQuestion(
            @Parameter(description = "题目ID") @PathVariable Long id,
            @RequestBody @Valid TestQuestionDTO dto) {
        return Result.success(testQuestionService.updateQuestion(id, dto));
    }

    @Operation(summary = "删除题目")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteQuestion(@PathVariable Long id) {
        return Result.success(testQuestionService.deleteQuestion(id));
    }

    @Operation(summary = "根据测试ID获取题目列表")
    @GetMapping("/test/{testId}")
    public Result<List<TestQuestionVO>> getQuestionsByTestId(
            @Parameter(description = "测试ID") @PathVariable Long testId) {
        return Result.success(testQuestionService.getQuestionsByTestId(testId));
    }

    @Operation(summary = "根据维度获取题目列表")
    @GetMapping("/dimension/{dimension}")
    public Result<List<TestQuestionVO>> getQuestionsByDimension(
            @Parameter(description = "维度") @PathVariable String dimension,
            @Parameter(description = "测试ID") @RequestParam Long testId) {
        List<TestQuestionVO> voList = testQuestionService.getQuestionsByDimension(testId, dimension)
                .stream()
                .map(question -> {
                    TestQuestionVO vo = new TestQuestionVO();
                    org.springframework.beans.BeanUtils.copyProperties(question, vo);
                    return vo;
                })
                .collect(java.util.stream.Collectors.toList());
        return Result.success(voList);
    }

    @Operation(summary = "批量导入题目")
    @PostMapping("/import")
    public Result<Void> importQuestions(
            @RequestParam("file") MultipartFile file,
            @RequestParam("testId") Long testId) {
        try {
            if (testId == null) {
                return Result.error("请选择所属测试");
            }
            testQuestionService.importQuestions(file, testId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("导入失败：" + e.getMessage());
        }
    }
} 